package com.itqf.service.impl;

import com.itqf.entity.TbItem;
import com.itqf.entity.TbItemDesc;
import com.itqf.mapper.TbItemMapper;
import com.itqf.service.TbItemDescService;
import com.itqf.service.TbItemService;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2021/1/18
 * @Time: 上午9:57
 */
public class TbItemServiceImpl implements TbItemService {

    //依赖
    private TbItemMapper tbItemMapper ;//假如有实现类：=new TbItemMapperIMpl();  强耦合

    private TbItemDescService tbItemDescService;//

    public void setTbItemDescService(TbItemDescService tbItemDescService) {
        this.tbItemDescService = tbItemDescService;
    }

    //注入
    public void setTbItemMapper(TbItemMapper tbItemMapper) {
        this.tbItemMapper = tbItemMapper;
    }

    /**
     * 事务
     * @param tbItem
     * @return
     */
    @Override
    public int addTbItem(TbItem tbItem) {

        int i= tbItemMapper.insert(tbItem);

        int j = 10/0;

        //addTbItemDesc 商品详情表
        //

        return i;
    }


    @Override
    public int addTbItemAndPropagation(TbItem tbItem) {//异常链  a-->b-->c
        int i = tbItemMapper.insert(tbItem);//返回自增主键

        //调用另一个业务类的方法  事务怎么传播的？？？
        TbItemDesc tbItemDesc  = new TbItemDesc();
        tbItemDesc.setItemId(tbItem.getId());
        tbItemDesc.setItemDesc("就是好");

        ////REQUIRED 跟addTbItemAndPropagation事务有关
        // addTbItemAndPropagation有事务，insert 应用该事务，有异常一起回滚
        //addTbItemAndPropagation没有事务，insert 开启一个事务，有异常回滚insert方法

        try {//异常链
            //NESTED  保存点方式的事务  insert有异常 只回滚insert方法
            //
            // 有异常只回滚insert方法
            i+=tbItemDescService.insert(tbItemDesc);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //int k = 10/0;

        return  i;

    }
}
